package com.google.code.seditor.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;

public class FileDao extends CommonDao {

    public static final String KEY_TITLE = "title";
    public static final int INDEX_TITLE = 0;
    public static final String KEY_PATH = "path";
    public static final int INDEX_PATH = 1;
    public static final String KEY_ROWID = "_id";
    
    public static final String TABLE_NAME = "fileinfo";
    
    /**
     * Create a new note using the title and body provided. If the note is
     * successfully created return the new rowId for that note, otherwise return
     * a -1 to indicate failure.
     * 
     * @param title the title of the note
     * @param path the body of the note
     * @return rowId or -1 if failed
     */
    public long createNote(String title, String path) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_PATH, path);

        return getDB().insert(TABLE_NAME, null, initialValues);
    }

    /**
     * Delete the note with the given rowId
     * 
     * @param rowId id of note to delete
     * @return true if deleted, false otherwise
     */
    public boolean deleteNote(long rowId) {

        return getDB().delete(TABLE_NAME, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
     * Return a Cursor over the list of all notes in the database
     * 
     * @return Cursor over all notes
     */
    public Cursor fetchAllNotes() {

        return getDB().query(TABLE_NAME, new String[] {KEY_ROWID, KEY_TITLE,
                KEY_PATH}, null, null, null, null, null);
    }

    /**
     * Return a Cursor positioned at the note that matches the given rowId
     * 
     * @param rowId id of note to retrieve
     * @return Cursor positioned to matching note, if found
     * @throws SQLException if note could not be found/retrieved
     */
    public Cursor fetchNote(long rowId) throws SQLException {

        Cursor mCursor = getDB().query(true, TABLE_NAME, new String[] {KEY_ROWID,
                    KEY_TITLE, KEY_PATH}, KEY_ROWID + "=" + rowId, null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

    /**
     * Update the note using the details provided. The note to be updated is
     * specified using the rowId, and it is altered to use the title and body
     * values passed in
     * 
     * @param rowId id of note to update
     * @param title value to set note title to
     * @param path value to set note path to
     * @return true if the note was successfully updated, false otherwise
     */
    public boolean updateNote(long rowId, String title, String path) {
        ContentValues args = new ContentValues();
        args.put(KEY_TITLE, title);
        args.put(KEY_PATH, path);

        return getDB().update(TABLE_NAME, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}
